package ae.pcfc.cldos.online.common.strategy.mail.sender;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import ae.pcfc.cldos.online.common.manager.FileStorageManager;
import ae.pcfc.cldos.online.common.vo.FileVO;
import ae.pcfc.cldos.online.gs.service.client.GovtSysGS;
import ae.pcfc.cldos.online.gs.service.client.vo.NotificationVO;
import ae.pcfc.cldos.online.net.service.vo.AttachmentVO;

/**
 * 
 * @author arosa
 *
 */
public class MailToCompanySenderThread extends Thread{
	
	private GovtSysGS govtSysGS;
	private NotificationVO notificationVO;
	private FileStorageManager fileStorageManager;
	
	public MailToCompanySenderThread(GovtSysGS govtSysGS, NotificationVO notificationVO, FileStorageManager fileStorageManager){
		this.fileStorageManager = fileStorageManager;
		this.govtSysGS = govtSysGS;
		this.notificationVO = notificationVO;
	}
	
	public void run() {
		List<AttachmentVO> attachmentVOs = notificationVO.getAttachments();
		List<String> dotNetIds = new ArrayList<String>(attachmentVOs.size());
		for (AttachmentVO attachmentVO : attachmentVOs) {
			FileVO fileVO = new FileVO();
			fileVO.setApplicationId(Long.parseLong(attachmentVO.getApplicationID()));
			fileVO.setAttachmentTypeCode(attachmentVO.getFileType());
			fileVO.setFileExt(attachmentVO.getExtension());
			fileVO.setFileName(attachmentVO.getFileName());
			fileVO.setId(Long.parseLong(attachmentVO.getOnlineID()));
			try {
				Long donNetId = fileStorageManager.transferToDotNet(fileVO);
				dotNetIds.add(donNetId.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		notificationVO.setTransferedAttachmentsIds(dotNetIds);
		boolean success = govtSysGS.sendNotificationAndClientEmail(notificationVO);
		if(success){
			System.out.println("Email successfuly sended");
		} else {
			System.out.println("Email not sended");
		}
	}
}
